/*****************************************************************************
 * backtrace_posix.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define BT_BUF_SIZE 100
/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
void cal_backtrace_show(void)
{
    int    j, nptrs;
    void * buffer[BT_BUF_SIZE];
    char **strings;

    nptrs = backtrace(buffer, BT_BUF_SIZE);
    printf("backtrace() returned %d addresses\n", nptrs);

    /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
        would produce similar output to the following: */

    strings = backtrace_symbols(buffer, nptrs);
    if (strings == NULL) {
        perror("backtrace_symbols");
        exit(EXIT_FAILURE);
    }

    for (j = 0; j < nptrs; j++)
        printf("[%d]:%s\n", j, strings[j]);

    free(strings);
}

PLUM_PRIVATE
void cal_backtrace(const char *fmt, ...)
{
    char    fbuf[128];
    va_list va;
    va_start(va, fmt);
    (void)vsnprintf(fbuf, sizeof(fbuf), fmt, va);
    va_end(va);

    printf("********************************************************\n");
    printf("*                        Panic                         *\n");
    printf("*        This message is indicative of a BUG.          *\n");
    printf("********************************************************\n");
    cal_backtrace_show();
    printf("********************************************************\n");
}

PLUM_PRIVATE
plum_void cal_signal_handler(plum_s32 signo)
{
    /** 打印调用栈信息 */
    cal_backtrace("PLUM");

    /** 恢复信号默认处理方式 */
    signal(signo, SIG_DFL);

    /** 重新发送信号 */
    raise(signo);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_void plum_cal_backtrace(plum_void)
{
    signal(SIGSEGV, cal_signal_handler);
}

/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
